//
//  ByCellSafetyOrder.m
//  chequanbao_ios
//
//  Created by pro on 2017/6/20.
//  Copyright © 2017年 com.itboye. All rights reserved.
//

#import "ByCellSafetyOrder.h"

// vc
#import "ByUISafetyPayRecordVC.h"

@implementation ByCellSafetyOrder
{
    UIView *_bgView;
    UILabel *_nameLabel;        // 姓名
    UILabel *_cartNoLabel;      // 车牌号
    UILabel *_typeLabel;        // 类型（财产保险）
//    UILabel *_subtypeLabel;     // 子类型（车险）
    UILabel *_moneyLabel;       // 金额
    UILabel *_dataLabel;        // 日期
    UILabel *_mobileLabel;      // 手机
    UILabel *_statusLabel;      // 状态
    UIButton *_uploadButton;    // 上传
    UIButton *_lookButton;      // 查看支付信息
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = [UIColor clearColor];
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    
    _bgView = [[UIView alloc] init];
    _bgView.backgroundColor = [UIColor colorWithWhite:1 alpha:0.2];
    _bgView.layer.cornerRadius = 4.0;
    [self.contentView addSubview:_bgView];
    
    _nameLabel = [[UILabel alloc] init];
    _nameLabel.textColor = [UIColor whiteColor];
    _nameLabel.font = FONT(15);
    _nameLabel.textAlignment = NSTextAlignmentLeft;
    [_bgView addSubview:_nameLabel];
    
    _cartNoLabel = [[UILabel alloc] init];
    _cartNoLabel.textColor = [UIColor whiteColor];
    _cartNoLabel.font = FONT(15);
    _cartNoLabel.textAlignment = NSTextAlignmentLeft;
    [_bgView addSubview:_cartNoLabel];

    _typeLabel = [[UILabel alloc] init];
    _typeLabel.adjustsFontSizeToFitWidth = YES;
    _typeLabel.font = FONT(15);
    _typeLabel.textColor = [UIColor whiteColor];
    _typeLabel.textAlignment = NSTextAlignmentLeft;
    [_bgView addSubview:_typeLabel];
    
//    _subtypeLabel = [[UILabel alloc] init];
//    _subtypeLabel.adjustsFontSizeToFitWidth = YES;
//    _subtypeLabel.font = FONT(15);
//    _subtypeLabel.textColor = [UIColor whiteColor];
//    _subtypeLabel.textAlignment = NSTextAlignmentLeft;
//    [_bgView addSubview:_subtypeLabel];
    
    _moneyLabel = [[UILabel alloc] init];
    _moneyLabel.adjustsFontSizeToFitWidth = YES;
    _moneyLabel.font = FONT(15);
    _moneyLabel.textColor = rgba(247, 233, 83, 1);
    _moneyLabel.textAlignment = NSTextAlignmentCenter;
    [_bgView addSubview:_moneyLabel];
    
    _dataLabel = [[UILabel alloc] init];
    _dataLabel.adjustsFontSizeToFitWidth = YES;
    _dataLabel.font = FONT(14);
    _dataLabel.textColor = [UIColor whiteColor];
    _dataLabel.textAlignment = NSTextAlignmentLeft;
    [_bgView addSubview:_dataLabel];
    
    _mobileLabel = [[UILabel alloc] init];
    _mobileLabel.adjustsFontSizeToFitWidth = YES;
    _mobileLabel.font = FONT(14);
    _mobileLabel.textColor = [UIColor whiteColor];
    _mobileLabel.textAlignment = NSTextAlignmentCenter;
    [_bgView addSubview:_mobileLabel];
    
    _statusLabel = [[UILabel alloc] init];
    _statusLabel.adjustsFontSizeToFitWidth = YES;
    _statusLabel.font = FONT(14);
    _statusLabel.textColor = [UIColor whiteColor];
    _statusLabel.textAlignment = NSTextAlignmentLeft;
    [_bgView addSubview:_statusLabel];
    
    
    _uploadButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _uploadButton.titleLabel.font = FONT(14);
    [_uploadButton addTarget:self action:@selector(upload) forControlEvents:UIControlEventTouchUpInside];
    _uploadButton.hidden = YES;
    [_uploadButton setTitle:@"上传" forState:UIControlStateNormal];
    [_uploadButton setTitleColor:[UIColor cyanColor] forState:UIControlStateNormal];
    [_bgView addSubview:_uploadButton];
    
    _lookButton = [[UIButton alloc] init];
    _lookButton.titleLabel.font = FONT(14);
    [_lookButton setTitle:@"查看支付信息" forState:UIControlStateNormal];
    [_lookButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _lookButton.backgroundColor = [UIColor colorWithR:31 g:163 b:239];
    _lookButton.layer.cornerRadius = 20 / 2;
    _lookButton.layer.masksToBounds = YES;
    [_bgView addSubview:_lookButton];
}

-(void)setModel:(ByModelSafetyOrder *)model{
    _model = model;
    [self setNeedsLayout];
}

- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    CGFloat x, y, width, height;
    x = 10;
    y = 0;
    width = self.width - x * 2;
    height = self.height;
    _bgView.frame = CGRectMake(x, y, width, height);
    
    // 车牌号
    x = 0;
    y = 10;
    width = [self.model.car_code getSizeOfString:15].width + 5;
    height = 20;
    _cartNoLabel.frame = CGRectMake(x, y, width, height);
//    _cartNoLabel.backgroundColor = [UIColor yellowColor];
    
    // 名字
    x = 10;
    y = 10;
    width = [self.model.name getSizeOfString:15].width + 5;
    width = MIN(width, _bgView.width - _cartNoLabel.width - 20);
    height = 20;
    _nameLabel.frame = CGRectMake(x, y, width, height);
    _cartNoLabel.left = _nameLabel.right + 10;
//    _nameLabel.backgroundColor = [UIColor redColor];
    

    // 类型
    x = 10;
    y = _nameLabel.bottom + 10;
    width = (_bgView.width - x * 2) / 4;
    height = 20;
    _typeLabel.frame = CGRectMake(x, y, width, height);
    
    // 价格
    x = _typeLabel.right;
    y = _typeLabel.top;
    width = _typeLabel.width;
    height = _typeLabel.height;
    _moneyLabel.frame = CGRectMake(x, y, width, height);
    
    // 时间
    x = 10;
    y = _typeLabel.bottom + 10;
    width = _typeLabel.width * 1.5;
    height = _typeLabel.height;
    _dataLabel.frame = CGRectMake(x, y, width, height);
    
    // 手机号
    x = _dataLabel.right;
    y = _dataLabel.top;
    width = _typeLabel.width;
    height = _dataLabel.height;
    _mobileLabel.frame = CGRectMake(x, y, width, height);
    
    // 查看支付信息
    x = 0;
    y = _dataLabel.top;
    width = [@"查看支付信息" getSizeOfString:14].width + 10;
    height = _dataLabel.height;
    _lookButton.frame = CGRectMake(x, y, width, height);
    _lookButton.right = _bgView.width - 10;
    
    // 上传
    x = 0;
    y = 0;
    width = 40;
    height = 18;
    _uploadButton.frame = CGRectMake(x, y, width, height);
    _uploadButton.y = _bgView.height / 2;
    _uploadButton.right = _bgView.width - 10;
    _uploadButton.layer.cornerRadius = height / 2;
    _uploadButton.layer.masksToBounds = YES;
    _uploadButton.layer.borderColor = [UIColor cyanColor].CGColor;
    _uploadButton.layer.borderWidth = 1;
    
    // 状态
    x = _mobileLabel.right + 5;
    y = _uploadButton.top;
    width = _uploadButton.left - x;
    height = _uploadButton.height;
    _statusLabel.frame = CGRectMake(x, y, width, height);
    
    
    
    /*
     模拟数据
     */
    //    NSString *type = @"财产保险";
    //    NSString *subtype = @"车险";
    //    NSString *money = @"300元";
    //    NSString *date = @"2017-05-05 15:24";
    //    NSString *mobile = @"12345678999";
    //    NSString *status = @"验证失败";
    _nameLabel.text = self.model.apply_name;
    _cartNoLabel.text = self.model.car_code;
    _typeLabel.text = [NSString stringWithFormat:@"%@(%@)",self.model.father_name,self.model.name];
    //    _subtypeLabel.text = self.model.name;
    _moneyLabel.text = [NSString stringWithFormat:@"%.2f元",self.model.amount.floatValue/100.0];
    _dataLabel.text = [NSString stringWithFormat:@"%@",[NSDate getDateStringFromeNumber:[NSNumber numberWithInteger:[self.model.update_time  integerValue]]]];
    _mobileLabel.text = self.model.mobile;
    switch (self.model.status_type.intValue) {
        case 1:
            _statusLabel.text = @"待确认";
            _statusLabel.textColor = [UIColor yellowColor];
            _uploadButton.hidden = YES;
            _lookButton.hidden = YES;
            break;
        case 2:
            _statusLabel.text = @"待上传";
            _statusLabel.textColor = [UIColor yellowColor];
            _uploadButton.hidden = NO;
            _lookButton.hidden = NO;
            break;
        case 3:
            _statusLabel.text = @"审核中";
            _statusLabel.textColor = [UIColor yellowColor];
            _uploadButton.hidden = NO;
            _lookButton.hidden = NO;
            break;
        case 4:
            _statusLabel.text = @"已完成";
            _statusLabel.textColor = [UIColor lightGrayColor];
            _uploadButton.hidden = YES;
            _lookButton.hidden = YES;
            break;
        case 5:
            _statusLabel.text = @"未通过";
            _statusLabel.textColor = [UIColor colorWithR:229 g:80 b:39];
            _uploadButton.hidden = YES;
            _lookButton.hidden = YES;
            break;
        default:
            break;
    }
    
    [_lookButton addTarget:self action:@selector(lookAction) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark -
- (void)lookAction{
    ByUISafetyPayRecordVC *vc = [[ByUISafetyPayRecordVC alloc] init];
    vc.insurance_id = _model.insurance_id;
    [(ByBaseVC *)self.viewController by_normalPush:vc hasNavi:YES];
}

- (void)upload {
    
    if (self.uploadBlock) {
        self.uploadBlock();
    }
}

@end
